package com.cloudfast.oauth.endpoint;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;

/**
 * 控制层异常类
 *
 * @author liuyw
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 框架异常处理类
     *
     * @param e
     * @return
     */
    @SuppressWarnings("static-access")
    @ResponseBody
    @ExceptionHandler(CloudException.class)
    @ResponseStatus()
    public ApiResponse handle(CloudException e) {
        ApiResponse ApiResponse = null;
        if (e.getException() instanceof CloudException) {
            CloudException cloudException = (CloudException) e.getException();
            if (cloudException.getCode() == ErrorCode.RE_LOGIN.getCode()) {
                ApiResponse = ApiResponse.message(ErrorCode.RE_LOGIN);
            } else if (cloudException.getCode() == ErrorCode.AUTHORIZATION_ERROR.getCode()) {
                ApiResponse = ApiResponse.message(ErrorCode.AUTHORIZATION_ERROR);
            } else {
                ApiResponse = ApiResponse.message(ErrorCode.SYSTEM_ERROR);
            }

        } else {
            ApiResponse = ApiResponse.message(e.getErrorCode());
        }
        // 抛出异常时返回的信息
        return ApiResponse;

    }
}